package com.woniuxy01;

import java.sql.*;
import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-06-16 11:34
 */
public class Test01 {
    public static void main(String[] args)  throws Exception{
        test05();
    }


    private static void test05() throws SQLException {
        Scanner scanner=new Scanner(System.in);
        //数据库操作四大参数
        //5.x   com.mysql.jdbc.Driver
        //8.x以后 com.mysql.cj.jdbc.Driver
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        //使用反射加载驱动类,可以省，默认会自动加载驱动包
//        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入学号");
        int sid = scanner.nextInt();
        System.out.println("输入姓名");
        String sname = scanner.next();
        System.out.println("输入年龄");
        int age = scanner.nextInt();
        String sql="insert into stu values(?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,sid);
        statement.setString(2,sname);
        statement.setInt(3,age);
        int count = statement.executeUpdate();
        System.out.println(count);
        statement.close();
        connection.close();
    }

    private static void test04() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入学号");
        int sid = scanner.nextInt();
        String sql="delete from stu where sid="+sid;
        Statement statement = connection.createStatement();
        int count = statement.executeUpdate(sql);
        System.out.println(count);
        statement.close();
        connection.close();
    }

    private static void test03() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入学号");
        int sid = scanner.nextInt();
        System.out.println("输入姓名");
        String sname = scanner.next();
        System.out.println("输入年龄");
        int age = scanner.nextInt();
        String sql="update stu set sname='"+sname+"',age="+age+" where sid="+sid;
        Statement statement = connection.createStatement();
        int count = statement.executeUpdate(sql);
        System.out.println(count);
        statement.close();
        connection.close();
    }

    private static void test02() {
        Connection connection=null;
        Statement statement =null;
        try {
            String className="com.mysql.cj.jdbc.Driver";
            Class.forName(className);
            String url="jdbc:mysql:///db1";
            String user="root";
            String password="123";
            connection = DriverManager.getConnection(url, user, password);
            statement = connection.createStatement();
            String sql="insert into stu values(5,'zs',11)";
            int count = statement.executeUpdate(sql);
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void test01() throws SQLException {
        Scanner scanner=new Scanner(System.in);
        //数据库操作四大参数
        //5.x   com.mysql.jdbc.Driver
        //8.x以后 com.mysql.cj.jdbc.Driver
        String className="com.mysql.cj.jdbc.Driver";
        //连接规范
//        String url="jdbc:mysql://localhost:3306/db1";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        //使用反射加载驱动类,可以省，默认会自动加载驱动包
//        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入学号");
        int sid = scanner.nextInt();
        System.out.println("输入姓名");
        String sname = scanner.next();
        System.out.println("输入年龄");
        int age = scanner.nextInt();
        String sql="insert into stu values("+sid+",'"+sname+"',"+age+")";
        Statement statement = connection.createStatement();
        int count = statement.executeUpdate(sql);
        System.out.println(count);
        statement.close();
        connection.close();
    }
}
